package zhihuilan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import zhihuilan.common.enums.RedisKeyEnum;
import zhihuilan.common.utils.RedisCache;
import zhihuilan.entity.MyConfig;
import zhihuilan.mapper.MyConfigMapper;
import zhihuilan.service.MyConfigService;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 用户数据 服务实现
 *
 * @author ruoyi
 */
@Service
public class MyConfigServiceImpl extends ServiceImpl<MyConfigMapper, MyConfig> implements MyConfigService {

    @Resource
    private RedisCache redisCache;

    @Override
    public List<MyConfig> selectList(String name, Integer type, Integer group) {
        QueryWrapper<MyConfig> queryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(name)) {
            queryWrapper.eq("name", name);
        }
        if (!ObjectUtils.isEmpty(type)) {
            queryWrapper.eq("type", type);
        }
        if (!ObjectUtils.isEmpty(group)) {
            queryWrapper.eq("group", group);
        }
        queryWrapper.eq("status", 1);
        List<MyConfig> result = this.baseMapper.selectList(queryWrapper);
        return result;
    }

    @Override
    public MyConfig selectByName(String name) {
        String key = RedisKeyEnum.REDIS_KEY_MYCONFIG + name;
        MyConfig myConfig = redisCache.getCacheObject(key);
        if (ObjectUtils.isEmpty(myConfig)) {
            QueryWrapper<MyConfig> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("name", name);
            queryWrapper.eq("status", 1);
            myConfig = this.baseMapper.selectOne(queryWrapper);
            if (!ObjectUtils.isEmpty(myConfig)) {
                redisCache.setCacheObject(key, myConfig, 2, TimeUnit.HOURS);
            }
        }
        return myConfig;
    }
}
